package com.example.recyclerviewcheckitemdemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.LinkedList;

public class CheckItemAdapter extends RecyclerView.Adapter<CheckItemAdapter.ViewHolder>{

    private final Context context;
    private final LinkedList<Item> items = new LinkedList<>();
    private final CheckItemListener checkItemListener;

    public CheckItemAdapter(Context context, CheckItemListener checkItemListener) {
        this.context = context;
        this.checkItemListener = checkItemListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addItem(Item item){
        this.items.addLast(item);
        notifyDataSetChanged();
    }

    public void removeItem(int position){
        this.items.remove(position);
        notifyItemRemoved(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_check_item,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.mask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int layoutPosition = holder.getLayoutPosition();
                for (int i = 0; i < items.size(); i++) {
                    items.get(i).check= layoutPosition == i;
                    if (items.get(i).check){
                        if (checkItemListener!=null){
                            checkItemListener.onCheckItem(i);
                        }
                    }
                }
                //这里用view的handler发消息队列去刷新 不然会报错
                view.post(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        notifyDataSetChanged();
                    }
                });
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = this.items.get(position);
        holder.checkBox.setChecked(item.check);
    }

    @Override
    public int getItemCount() {
        return this.items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        public final CheckBox checkBox;
        private final View mask;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.check);
            mask = itemView.findViewById(R.id.mask);
        }
    }


    public interface CheckItemListener{
        void onCheckItem(int position);
    }


    static class Item{
        public boolean check;

        public Item(boolean check) {
            this.check = check;
        }
    }
}
